﻿/* 
Description :Js兼容类
Nickname :Irving 
Date:2012年2月25日12:08:05
Email:zhouyongtao@outlook.com
*/
var Util = {
    //是否为IE
    getIE: function () {
        return window.attachEvent ? true : false;
    },
    // 获取变量数据类型 
    getType: function (obj) {
        return (obj === null) ? "null" : typeof (obj);
    },
    //根据元素id获得元素
    getID: function (elementId) {
        return Util.getType(elementId) === "string" ? document.getElementById(elementId) : elementId;
    },
    //根据元素标签名获取元素
    getName: function (elementName) {
        return document.getElementsByName(elementName);
    },
    //根据元素标签名获取元素集合
    getTag: function (elementName) {
        return document.getElementsByTagName(elementName);
    },
    // 得到输入
    getCode: function (e) {
        e = EventUtil.getEvent(e);
        return e.keyCode || e.which;
    },
    //数值排序
    sortNumByAsc: function (a, b) {
        return a - b;
    },
    sortNumByDesc: function (a, b) {
        return b - a;
    },
    //多键排序 (def可选参数)
    sortByTagName: function (name, def) {
        return function (o, p) {
            var a, b;
            if (o && p && typeof o === 'object' && typeof p === 'object') {
                a = o[name];
                b = p[name];
                if (a === b) {
                    return typeof def === 'function' ? def(o, p) : 0;
                }
                if (typeof a === typeof b) {
                    return a < b ? -1 : 1;
                }
                return typeof a < typeof b ? -1 : 1;
            } else {
                throw ("error");
            }
        }
    }
};

var EventUtil = {
    //监听事件(flag参数可选)
    addEventListener: function (elementName, eventName, handler, flag) {
        var element = Util.getID(elementName);
        //var element = (elementName === "document.body") ? elementName : Util.getID(elementName);
        if (flag) {
            Util.getIE() ? element.attachEvent('on' + eventName, handler) : element.addEventListener(eventName, handler, true); //冒泡
        }
        else {
            Util.getIE() ? element.attachEvent('on' + eventName, handler) : element.addEventListener(eventName, handler, false); //捕获
        }

    },
    //移除事件
    removeEventListener: function (elementName, eventName, handler) {
        var element = Util.getID(elementName);
        Util.getIE() ? element.detachEvent('on' + eventName, handler) : element.removeEventListener(eventName, handler, false);
    },
    //事件模拟(MouseEvents)
    simulateMouseEvent: function (elementName, eventName) {
        var element = Util.getID(elementName);
        if (Util.getIE()) {
            element.fireEvent("on" + eventName);
        }
        else {
            var evt = document.createEvent("MouseEvents");
            evt.initMouseEvent(eventName, true, true, window, 0, 0, 0, 0, 0, false, false, false, false, 0, null);
            element.dispatchEvent(evt);
        }
    },
    //获取事件引用(IE9已修正)
    getEvent: function (e) {
        return e || window.event;
    },
    //获取事件源对象
    getTarget: function (e) {
        e = EventUtil.getEvent(e);
        return e.target || e.srcElement;
    },
    // 取消事件
    preventDefault: function (e) {
        e = EventUtil.getEvent(e);
        e.preventDefault ? e.preventDefault() : e.returnValue = false;
    },
    //阻止冒泡
    stopPropagation: function (e) {
        e = EventUtil.getEvent(e);
        e.stopPropagation ? e.stopPropagation() : e.cancelBubble = true;
    }
};

var FormUtil = {
    //响应键盘回车事件(EasyUI表单 keyup, keydown)
    autoEasyuiSubmit: function (formName) {
        $("#" + formName).bind("keyup", function (e) {
            if (Util.getCode(e) === "13") {
                $("#" + formName).submit();
            }
        });
    },
    //响应键盘回车事件
    autoSubmit: function (elementName) {
        EventUtil.addEventListener("body", "keyup", function (e) {
            if (Util.getCode(e) === 13)
                EventUtil.simulateMouseEvent(elementName, "click"); //需要提前绑定click的Ajax事件
        });
    }
};

var ValidateUtil = {
    /*
    * 根据手机号码获取运营商的序号
    * @param { Number } 11位手机号码
    * @return { Number } 各运营商对应的序号 
    * 0 : 移动，1 : 联通，2 ： 电信，-1 : 号码错误
    */
    getISP: function (number) {
        var rCMCC = /^(139|138|137|136|135|134|159|158|152|151|150|157|188|187|147|182|183)[0-9]{8}$/, // 移动
			rUNICOM = /^(130|131|132|155|156|186|185)[0-9]{8}$/, // 联通
			rCT = /^(133|153|189|180)[0-9]{8}$/; // 电信
        return rCMCC.test(number) ? 0 :
			rUNICOM.test(number) ? 1 :
			rCT.test(number) ? 2 :
			-1;
    }
}

var BomUtil = {
    getSize: function () {
        var windowHeight = 0;
        var widowWidth = 0;
        if (typeof (window.innerHeight) === 'number') {
            windowHeight = window.innerHeight;
            widowWidth = window.innerWidth;
        }
        else {
            if (document.documentElement && document.documentElement.clientHeight) {
                windowHeight = document.documentElement.clientHeight;
                widowWidth = document.documentElement.clientWidth;
            }
            else {
                if (document.body && document.body.clientHeight) {
                    windowHeight = document.body.clientHeight;
                    widowWidth = document.body.clientWidth;
                }
            }
        }
        return { width: widowWidth, height: windowHeight };
    }
}


function setcookie(cookieName, cookieValue, seconds, path, domain, secure) {
    var expires = new Date();
    expires.setTime(expires.getTime() + seconds);
    document.cookie = escape(cookieName) + '=' + escape(cookieValue)
+ (expires ? '; expires=' + expires.toGMTString() : '')
+ (path ? '; path=' + path : '/')
+ (domain ? '; domain=' + domain : '')
+ (secure ? '; secure' : '');
}

function getcookie(name) {
    var cookie_start = document.cookie.indexOf(name);
    var cookie_end = document.cookie.indexOf(";", cookie_start);
    return cookie_start == -1 ? '' : unescape(document.cookie.substring(cookie_start + name.length + 1, (cookie_end > cookie_start ? cookie_end : document.cookie.length)));
}


//只能输入数字
function testKeyEven(e) {
    if (window.attachEvent) {
        if ((e.keyCode > 95 && e.keyCode < 106) || (e.keyCode > 47 && e.keyCode < 58)) {
            return true;
        }
        return false;
    }
    else {
        if ((e.which > 95 && e.which < 106) || (e.which > 47 && e.which < 58)) {
            return true;
        }
        return false;
    }
} 